package cn.edu.cdu.farmdeal.controller;

import cn.edu.cdu.farmdeal.domain.*;
import cn.edu.cdu.farmdeal.mapper.ProductMapper;
import cn.edu.cdu.farmdeal.service.OrderService;
import cn.edu.cdu.farmdeal.utils.Page;
import cn.edu.cdu.farmdeal.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.cdu.farmdeal.utils.RedisUtil;

/**
 * @Author: tupeng
 * @Date: 2020/4/1
 **/
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ProductMapper productMapper;

    @PostMapping("/order/confirmReceipt")
    @ResponseBody
    public R confirmReceipt(String id){
        return R.ok().put("data",orderService.confirmReceipt(Integer.parseInt(id)));
    }

    @GetMapping("/order/getOrderInfo")
    @ResponseBody
    public R getOrderInfo(Page page, Address address, Order order,HttpServletRequest request)
    {
        HttpSession session =  request.getSession();
        String username = (String) session.getAttribute("userid");
        int count = orderService.getTotalCount(username, order,address);
//        order.setTotalCount(count);/
        //获取此页码下的订单数据
        List<Map<String, Object>> orderList = orderService.getOrderInfo(username, page, order, address);
        HashMap map = new HashMap(10);
        map.put("count", count);
        map.put("data", orderList);
        return R.ok(map);
    }


    @RequestMapping("/cartCommit.html")
    public ModelAndView orderCommit(ModelAndView modelAndView,
                                    HttpServletRequest request,
                                    @RequestBody String[] ids)
    {
        HttpSession session1 = request.getSession();
        for (String i : ids) {
            System.out.println(i);
        }
        modelAndView.setViewName("orderCommit");
        return modelAndView;
    }

    @RequestMapping("/detailCommit.html")
    public ModelAndView orderCommitById(ModelAndView modelAndView,
                                        HttpServletRequest request,
                                        String id, String buySum) {
        modelAndView.setViewName("detailCommit");
        return modelAndView;
    }

    @PostMapping("/order/saveAddress")
    @ResponseBody
    public R saveAddress(@RequestBody Map<String, Object> map) {
        return R.ok().put("data", orderService.saveAddress(map));
    }


    @GetMapping("/address/checkAddress")
    @ResponseBody
    public Boolean checkAddress(String userid) {
        return  orderService.checkAddress(userid);
    }

    @PostMapping("/order/updateAddress")
    @ResponseBody
    public R updateAddress(@RequestBody Map<String, Object> map) {
        return R.ok().put("data", orderService.updateAddress(map));
    }

    @GetMapping("/order/getProById")
    @ResponseBody
    public R getProById(String id, String buySum) {
        return R.ok().put("data", orderService.getProById(id, buySum));
    }

    @PostMapping(value = "/order/orderCommits")
    @ResponseBody
    public R doOrder( HttpSession session, @RequestBody Map<String, Object> map) throws ClassCastException{
        String userid = (String) session.getAttribute("userid");
        session.setAttribute("orderMap",map);
        //判断库存 select miaosha_stock from miaoshagoods
        String id = (String) map.get("product_id");
        String buySum = (String) map.get("buy_sum");
        int stock = productMapper.getStock(Integer.parseInt(id));
        int surplus = stock - Integer.parseInt(buySum);
        if (surplus >= 0) {
            //说明有库存
             Order orderInfo = orderService.doOrderInfo(map);
//            Order orderInfo = orderService.saveOrderInfo(map);
//            List<UserProduct> list = orderService.getUserPro();
//            for (UserProduct userProduct: list){
//                if(userProduct.getUserInfoId())
//            }

            orderService.saveUserPro(userid,Integer.parseInt(id));
            //确认支付
            session.setAttribute("orderInfo", orderInfo);
            //modelAndView.setViewName("redirect:/orderpay.html");
            //假设现在是立即支付  引入支付宝
//            modelAndView.setViewName("redirect:/alipay/pay");
            //}
            //减少库存   update miaosha_goods set miaosha_stock = miaosha_stock -1 where miaoshagoods_id = 3
            //插入订单 insert into orderinfo(order_no,address_id,miaoshagoods_id,user_id,create_time,buy_count) VALUES()
        }
        return R.ok();
    }

    @PostMapping(value = "/order/personOrderCommits")
    @ResponseBody
    public R payOrder(ModelAndView modelAndView, HttpSession session, HttpServletResponse response,
                                @RequestBody Map<String, Object> map) throws  IOException {
        String userid = (String) session.getAttribute("userid");
        session.setAttribute("orderMap",map);
        int id = (int) map.get("product_id");
        int buySum = (int) map.get("buy_sum");
        int stock = productMapper.getStock(id);
        int surplus = stock - buySum;
        if (surplus >= 0) {
            //说明有库存
             Order orderInfo = orderService.saveOrderInfo(map);
            //保存order信息

            //加入相似度计算
//            orderService.saveUserPro(userid,id);
            //确认支付
            session.setAttribute("orderInfo", orderInfo);
//            modelAndView.setViewName("redirect:/alipay/pay");
            //假设现在是立即支付  引入支付宝
//            response.sendRedirect("/alipay/pay");
            //}
            //减少库存   update miaosha_goods set miaosha_stock = miaosha_stock -1 where miaoshagoods_id = 3
            //插入订单 insert into orderinfo(order_no,address_id,miaoshagoods_id,user_id,create_time,buy_count) VALUES()
        }
        return R.ok();
    }

}
